#ifndef _TREEALLOCATOR_H
#define	_TREEALLOCATOR_H

#include <memory>
#include <cstdlib>

namespace etu {
namespace common {

template <class T>
class TreeAllocator {
public:
    typedef size_t size_type;
    typedef ptrdiff_t difference_type;
    typedef T* pointer;
    typedef const T* const_pointer;
    typedef T& reference;
    typedef const T& const_reference;
    typedef T value_type;

    TreeAllocator() {
    }

    TreeAllocator(const TreeAllocator&) {
    }

    pointer allocate(size_type n, const void * = 0) {
        T* t = (T*) malloc(n * sizeof (T));
        return t;
    }

    void deallocate(void* p, size_type) {
        if (p) {
            free(p);
        }
    }

    pointer address(reference x) const {
        return &x;
    }

    const_pointer address(const_reference x) const {
        return &x;
    }

    TreeAllocator<T> & operator=(const TreeAllocator&) {
        return *this;
    }

    void construct(pointer p, const T& val) {
        new ((T*) p) T(val);
    }

    void destroy(pointer p) {
        p->~T();
    }

    size_type max_size() const {
        return size_t(-1);
    }

    template <class U>
    struct rebind {
        typedef TreeAllocator<U> other;
    };

    template <class U>
    TreeAllocator(const TreeAllocator<U>&) {
    }

    template <class U>
    TreeAllocator & operator=(const TreeAllocator<U>&) {
        return *this;
    }
};

}
}

#endif	/* _TREEALLOCATOR_H */

